package com.xinbochuang.machine.admin.controller.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseIn;
import com.xinbochuang.machine.admin.domain.system.SysStorehouseOut;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseInService;
import com.xinbochuang.machine.admin.service.system.ISysStorehouseOutService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.enums.StoreHouseStatus;
import com.xinbochuang.machine.common.enums.UserStatus;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * app库房接口
 *
 * @author 黄晓鹏
 * @date 2020-09-28 22:27
 */
@Api(value = "app库房接口", position = 100, protocols = "http")
@RestController
@RequestMapping("/api/storehouse")
public class AppStoreHouseController extends BaseController {

    @Resource
    private ISysStorehouseInService storehouseInService;

    @Resource
    private ISysStorehouseOutService storehouseOutService;

    private static final String inPrefix = "/in";
    private static final String outPrefix = "/out";

    /**
     * 根据二维码字符串获取入库信息
     *
     * @param code 二维码字符串
     * @return 入库信息
     */
    @ApiOperation(value = "根据二维码字符串获取入库信息", consumes = "application/json", response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "二维码字符串", name = "code", required = true, dataTypeClass = String.class),
    })
    @GetMapping(inPrefix + "/getInfo")
    public AjaxResult getInfo(String code) {
        if (StrUtil.isBlank(code)) {
            return AjaxResult.error("参数错误！");
        }
        QueryWrapper<SysStorehouseIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysStorehouseIn::getCode, code);
        SysStorehouseIn storehouseIn = storehouseInService.getOne(queryWrapper);
        if (UserStatus.DELETED.getCode().equals(storehouseIn.getDelFlag())) {
            return AjaxResult.error("该信息已删除！");
        }
        if (StoreHouseStatus.NOCHECK.getCode().equals(storehouseIn.getStatus())) {
            return AjaxResult.error("该信息未审核！");
        }
        if (StoreHouseStatus.NOPASS.getCode().equals(storehouseIn.getStatus())) {
            return AjaxResult.error("该信息未通过审核！");
        }
        return AjaxResult.data(storehouseIn);
    }

    /**
     * 入库
     *
     * @param storehouseIn 入库信息
     * @return 结果
     */
    @ApiOperation(value = "根据id进行入库操作，修改状态为入库，添加入库位置", consumes = "application/json", response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "入库信息", required = true, dataTypeClass = SysStorehouseIn.class),
    })
    @PutMapping(inPrefix + "/push")
    public AjaxResult push(@RequestBody SysStorehouseIn storehouseIn) {
        if (storehouseIn == null) {
            return AjaxResult.error("参数错误！");
        }
        storehouseIn.setStatus(StoreHouseStatus.IN.getCode());
        storehouseIn.setUpdateTime(DateUtil.now());
        return toAjax(storehouseInService.updateById(storehouseIn));
    }

    /**
     * 根据id将物品信息标记为不存在
     *
     * @param inId 物品信息id
     * @return 结果
     */
    @ApiOperation(value = "盘点，根据id将物品信息标记为不存在", consumes = "application/json", response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id", name = "inId", required = true, dataTypeClass = Integer.class),
    })
    @PutMapping(inPrefix + "/notExist/{inId}")
    public AjaxResult notExist(@PathVariable Integer inId) {
        if (inId == null) {
            return AjaxResult.error("参数错误！");
        }
        SysStorehouseIn storehouseIn = new SysStorehouseIn();
        storehouseIn.setInId(inId);
        storehouseIn.setIsExist("N");
        return toAjax(storehouseInService.updateById(storehouseIn));
    }

    /**
     * 根据二维码字符串获取出库信息
     *
     * @param code 二维码字符串
     * @return 出库信息
     */
    @ApiOperation(value = "根据二维码字符串获取出库信息", consumes = "application/json", response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "二维码字符串", name = "code", required = true, dataTypeClass = String.class),
    })
    @GetMapping(outPrefix + "/getInfo")
    public AjaxResult getOutInfo(String code) {
        if (StrUtil.isBlank(code)) {
            return AjaxResult.error("参数错误！");
        }
        QueryWrapper<SysStorehouseOut> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysStorehouseOut::getCode, code);
        SysStorehouseOut storehouseOut = storehouseOutService.getOne(queryWrapper);
        if (StoreHouseStatus.NOCHECK.getCode().equals(storehouseOut.getStatus())) {
            return AjaxResult.error("该信息未审核！");
        }
        if (StoreHouseStatus.NOPASS.getCode().equals(storehouseOut.getStatus())) {
            return AjaxResult.error("该信息未通过审核！");
        }
        return AjaxResult.data(storehouseOut);
    }

    /**
     * 出库
     *
     * @param storehouseOut 出库信息
     * @return 结果
     */
    @ApiOperation(value = "根据id进行出库操作，修改状态为出库，添加出库位置", consumes = "application/json", response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "出库信息", required = true, dataTypeClass = SysStorehouseOut.class),
    })
    @PutMapping(outPrefix + "/pull")
    public AjaxResult pull(@RequestBody SysStorehouseOut storehouseOut) {
        if (storehouseOut == null) {
            return AjaxResult.error("参数错误！");
        }
        return toAjax(storehouseOutService.pull(storehouseOut));
    }

}
